package com.bosssoft.g1.service.impl;

import com.bosssoft.g1.common.data.vo.CommonResponse;
import com.bosssoft.g1.dto.ResourceDTO;
import com.bosssoft.g1.entity.Resource;
import com.bosssoft.g1.entity.RoleResource;
import com.bosssoft.g1.entity.SubSystem;
import com.bosssoft.g1.entity.UserRole;
import com.bosssoft.g1.mapper.ResourceMapper;
import com.bosssoft.g1.mapper.RoleResourceMapper;
import com.bosssoft.g1.mapper.SubSystemMapper;
import com.bosssoft.g1.mapper.UserRoleMapper;
import com.bosssoft.g1.service.LoginService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import tk.mybatis.mapper.entity.Example;

import javax.print.ServiceUI;
import java.util.*;
import java.util.stream.Collectors;

@Service
@Validated
public class LoginServiceImpl implements LoginService {
    @Autowired
    private SubSystemMapper subSystemMapper;

    @Autowired
    private RoleResourceMapper roleResourceMapper;

    @Autowired
    private ResourceMapper resourceMapper;
    @Autowired
    private UserRoleMapper userRoleMapper;

    /**
     * 根据用户id查询子系统相关信息
     * @param id
     * @return
     */
    @Override
    public List<Map<String, Object>> getMenu(Long id) {
        // 根据用户id查询角色id
        Example userRoleExample = new Example(UserRole.class);
        userRoleExample.createCriteria().andEqualTo("userId", id);
        List<UserRole> userRoles = userRoleMapper.selectByExample(userRoleExample);

        List<Long> roleIds = userRoles.stream()
                .map(UserRole::getRoleId)
                .collect(Collectors.toList());

        if (roleIds.isEmpty()) {
            return Collections.emptyList();
        }

        // 根据角色id查询资源id
        Example roleResourceExample = new Example(RoleResource.class);
        roleResourceExample.createCriteria().andIn("roleId", roleIds);
        List<RoleResource> roleResources = roleResourceMapper.selectByExample(roleResourceExample);

        List<Long> resourceIds = roleResources.stream()
                .map(RoleResource::getResourceId)
                .collect(Collectors.toList());

        if (resourceIds.isEmpty()) {
            return Collections.emptyList();
        }

        // 根据资源id查询子系统id
        Example resourceExample = new Example(Resource.class);
        resourceExample.createCriteria().andIn("id", resourceIds);
        List<Resource> resources = resourceMapper.selectByExample(resourceExample);

        Set<Long> subsystemIds = resources.stream()
                .map(Resource::getSubSystemId)
                .collect(Collectors.toSet());

        if (subsystemIds.isEmpty()) {
            return Collections.emptyList();
        }

        // 根据子系统id查询子系统信息
        Example subsystemExample = new Example(SubSystem.class);
        subsystemExample.createCriteria().andIn("id", subsystemIds);
        List<SubSystem> subsystems = subSystemMapper.selectByExample(subsystemExample);

        List<Map<String, Object>> result = new ArrayList<>();

        for (SubSystem subsystem : subsystems) {
            Map<String, Object> subsystemMap = new HashMap<>();
            subsystemMap.put("id", subsystem.getId());
            subsystemMap.put("name", subsystem.getName());
            subsystemMap.put("code", subsystem.getCode());
            subsystemMap.put("image_url", subsystem.getImageUrl());
            subsystemMap.put("link", subsystem.getLink());
            result.add(subsystemMap);
        }

        return result;
    }

    /**
     * 根据子系统id查询资源信息
     * @param id
     * @return
     */
    @Override
    public List<ResourceDTO> getMenuBySubId(Long id) {
        // 根据子系统id查询资源id
        Example resourceExample = new Example(Resource.class);
        resourceExample.createCriteria().andEqualTo("subSystemId", id);
        List<Resource> resources = resourceMapper.selectByExample(resourceExample);
        if (resources.isEmpty()) {
            System.out.println("resources为空");
            return null;
        }
        List<Long> resourceIds = resources.stream()
                .map(Resource::getId)
                .collect(Collectors.toList());

        //根据资源id查询资源信息
        Example resExample = new Example(Resource.class);
        System.out.println("查询的资源id：" + resourceIds);
        resExample.createCriteria().andIn("id", resourceIds);
        List<Resource> res = resourceMapper.selectByExample(resExample);
        System.out.println("查询到的资源信息：" + res);
        List<ResourceDTO> menuDTOs = res.stream()
                // 过滤resource_type=0的资源
                .filter(menu -> menu.getResourceType() != 1)
                .map(menu -> {
                    ResourceDTO dto = new ResourceDTO();
                    dto.setId(menu.getId());
                    dto.setName(menu.getName());
                    dto.setUrl(menu.getUrl());
                    return dto;
                })
                .collect(Collectors.toList());
        System.out.println("映射后的DTO列表：" + menuDTOs);

        if (menuDTOs.isEmpty()) {
            System.out.println("menuDTOs为空");
            return null;
        } else {
            return menuDTOs;
        }

    }
}
